<?php

class ManageProduct extends CI_Controller {

	function __construct() {
		parent::__construct();
		$id = $this->session->userdata('id');
		$adminname = $this->session->userdata('adminname');
		$hash = $this->session->userdata('hash');

		$this->admin['name'] = "SHYAKEN";
		$this->admin['id'] = 1;
		$this->admin['hash'] = md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE);
		$this->load->model('product_m');
	}

	function index() {
		if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
			$this->load->library('pagination');
			$offset = $this->uri->segment(3);
			$limit = 12;
			$config['base_url'] = site_url('admin/manageproduct');
			$config['uri_segment'] = 3;
			$config['total_rows'] = $this->db->count_all('sanpham');
			$config['per_page'] = $limit;
			$this->pagination->initialize($config);
			$data['paginator'] = $this->pagination->create_links();

			$data['product'] = $this->product_m->getall($limit, $offset, 0);
			//$data['catalogue'] = $this->catalogue_m->get_catalogue();
			$data['option'] = -1;
			$this->load->view('admin/common/header');
			$this->load->view('admin/product', $data);
			$this->load->view('admin/common/footer');
		}else
			redirect(base_url() . 'admin/login');
	}

	function add() {
		if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
			if (isset($_POST['add']) && $_POST['add'] == 'Add') {

				$data['title'] = $this->input->post('title');
				$data['slug'] = strtolower(url_title($data['title']));
				$data['content'] = trim($this->input->post("contents"));
				$data['type'] = $this->input->post('type');
				$data['image'] = $this->_upload_files();
				if ($data['type'] == '') $data['type'] = 0;
				$data['description'] = $this->input->post('intro');
				$arr = array("\r\n", "\n", "\r");
				$data['content'] = str_replace($arr, '', $data['content']);
				//die(print_r($data));
				// if(!is_numeric(trim($data['order'])))
				//   $data['order'] = 0;
					if (trim($data['title']) == '' || trim($data['content']) == '' || trim($data['slug']) == '')
					redirect(base_url() . 'admin/manageproduct/add');
				$id = $this->product_m->add($data);
				if ($id > 0) {
					redirect(base_url() . 'admin/manageproduct');
				}
				else
					die('Lỗi thêm tin mới!');
			}else {

				$data['catlist'] = $this->product_m->get_cat();

				$this->load->view('admin/common/header');
				$this->load->view('admin/product_add',$data);
				//$this->load->view('admin/common/footer');
			}
		} else
			redirect(base_url() . 'admin/login');
	}

	function update($id) {
		if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
			if (!is_numeric($id) || $id < 0)
				redirect(base_url() . 'admin/manageproduct');

			if (isset($_POST['update']) && $_POST['update'] == 'Update') {
				$data['title'] = $this->input->post('title');
				$data['slug'] = strtolower(url_title($data['title']));
				$data['content'] = trim($this->input->post("contents"));
				//$data['status'] = $this->input->post('status');
				//$data['author'] = $this->input->post('author');
				$data['type'] = $this->input->post('type');
				if ($data['type'] == '') unset($data['type']);
				$data['description'] = $this->input->post('intro');
				$arr = array("\r\n", "\n", "\r");
				$data['content'] = str_replace($arr, '', $data['content']);
				$thumb = $this->_upload_files();
				if($thumb != "")
					$data['image'] = $thumb;
				if (trim($data['title']) == '' || trim($data['slug']) == '' || trim($data['content']) == '')
					redirect(base_url() . 'admin/manageproduct/update/' . $id);

				if ($this->product_m->update($id, $data)) {
					redirect(base_url() . 'admin/manageproduct');
				}
				else
					die('Lỗi cập nhật tin');
			}else {
				//die('xxx');
				$data['product'] = $this->product_m->get_product($id);
				$data['catalogue'] = $this->product_m->get_cat();

				$this->load->view('admin/common/header');
				$this->load->view('admin/product_edit', $data);
				$this->load->view('admin/common/footer');
			}
		} else
			redirect(base_url() . 'admin/login');
	}

	function delete($id) {
		if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
			if (!is_numeric($id))
				redirect(base_url() . 'admin/manageproduct');
			if ($this->product_m->delete($id)) {
				if (file_exists('uploads/product/' . $id)) {
					if (file_exists('uploads/product/' . $id . '/thumb.jpg'))
						unlink('uploads/product/' . $id . '/thumb.jpg');
					rmdir('uploads/product/' . $id);
				}
				redirect(base_url() . 'admin/manageproduct');
			}
			else
				die('Lỗi xóa tin');
		}else
			redirect(base_url() . 'admin/login');
	}

	function delete_product() {
		if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
			$ids = $this->input->post('selected');
			if (is_array($ids)) {
				if ($this->product_m->delete($ids))
					redirect(base_url() . 'admin/manageproduct');
				else
					die('Lỗi xóa tin');
			}else {
				redirect(base_url() . 'admin/manageproduct');
			}
		} else
			redirect(base_url() . 'admin/login');
	}

	function search() {
		if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
			if (isset($_POST['search']) && $_POST['search'] == 'Tìm kiếm') {

				$data['keyword'] = $this->input->post('txt_search');
				$this->session->set_userdata('keyword', $data['keyword']);

				if (trim($data['keyword']) == '')
					redirect(base_url() . 'admin/manageproduct');

				$this->load->library('pagination');
				$offset = $this->uri->segment(4);
				$limit = 12;
				$config['base_url'] = site_url('admin/manageproduct/search');
				$config['uri_segment'] = 4;
				$config['total_rows'] = $this->product_m->count_for_search($data['keyword']);
				$config['per_page'] = $limit;
				$this->pagination->initialize($config);
				$data['paginator'] = $this->pagination->create_links();

				$data['product'] = $this->product_m->search_result($data['keyword'], $limit, $offset);
				$data['option'] = -1;

				$this->load->view('admin/common/header');
				$this->load->view('admin/product', $data);
				$this->load->view('admin/common/footer');
			}else {

				$data['keyword'] = $this->session->userdata('keyword');
				if (!isset($data['keyword']) || $data['keyword'] == '')
					redirect(base_url() . 'admin/manageproduct');

				$this->load->library('pagination');
				$offset = $this->uri->segment(4);
				$limit = 12;
				$config['base_url'] = site_url('admin/manageproduct/search');
				$config['uri_segment'] = 4;
				$config['total_rows'] = $this->product_m->count_for_search($data['keyword']);
				$config['per_page'] = $limit;
				$this->pagination->initialize($config);
				$data['paginator'] = $this->pagination->create_links();

				$data['product'] = $this->product_m->search_result($data['keyword'], $limit, $offset);
				$data['option'] = -1;

				$this->load->view('admin/common/header');
				$this->load->view('admin/product', $data);
				$this->load->view('admin/common/footer');
			}
		} else
			redirect(base_url() . 'admin/login');
	}

	function catalogue() {
		if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
			if (isset($_POST['search_catalogue']) && $_POST['search_catalogue'] == 'Danh mục') {

				$data['option'] = $this->input->post('catalogue');
				$this->session->set_userdata('option', $data['option']);
				$data['order'] = $this->input->post('order');
				$this->session->set_userdata('order', $data['order']);
				if (trim($data['option']) == ""){
					$this->session->unset_userdata('option');
					$this->session->unset_userdata('order');
					redirect(base_url() . 'admin/manageproduct');
				}

				//die($data['option']);
				$this->load->library('pagination');
				$offset = $this->uri->segment(4);
				$limit = 12;
				$config['base_url'] = site_url('admin/manageproduct/catalogue');
				$config['uri_segment'] = 4;
				$config['total_rows'] = $this->product_m->count_catalogue($data['option']);
				$config['per_page'] = $limit;
				$this->pagination->initialize($config);
				$data['paginator'] = $this->pagination->create_links();

				$data['product'] = $this->product_m->get_product_catalogue($data['option'],$data['order'], $limit, $offset);
				$data['catalogue'] = $this->catalogue_m->get_catalogue();

				$this->load->view('admin/common/header');
				$this->load->view('admin/product', $data);
				$this->load->view('admin/common/footer');
			}else {
				$data['option'] = $this->session->userdata('option');
				$data['order'] = $this->session->userdata('order');
				if (!isset($data['option']) || $data['option'] == ""){
					$this->session->unset_userdata('option');
					$this->session->unset_userdata('order');
					redirect(base_url() . 'admin/manageproduct');
				}

				$this->load->library('pagination');
				$offset = $this->uri->segment(4);
				$limit = 12;
				$config['base_url'] = site_url('admin/manageproduct/catalogue');
				$config['uri_segment'] = 4;
				$config['total_rows'] = $this->product_m->count_catalogue($data['option']);
				$config['per_page'] = $limit;
				$this->pagination->initialize($config);
				$data['paginator'] = $this->pagination->create_links();

				$data['product'] = $this->product_m->get_product_catalogue($data['option'],$data['order'], $limit, $offset);
				$data['catalogue'] = $this->catalogue_m->get_catalogue();

				$this->load->view('admin/common/header');
				$this->load->view('admin/product', $data);
				$this->load->view('admin/common/footer');
			}
		} else
			redirect(base_url() . 'admin/login');
	}


	public function _upload_files() {
		if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
			if (!file_exists('uploads/product/thumbs')) {
				$umask = umask();
				umask(0);
				mkdir('uploads/product/thumbs', 0777,1);
				chmod('uploads/product/thumbs', 0777);
				umask($umask);
			}


			$upload_cfg['max_size'] = '2048';
			$upload_cfg['upload_path'] = 'uploads/product';
			$upload_cfg['allowed_types'] = 'gif|jpg|png';
			$upload_cfg['encrypt_name'] = FALSE;
			$upload_cfg['overwrite'] = FALSE;
			$this->load->library('upload', $upload_cfg);

			if($this->upload->do_upload('product_thumb')){
				$img = $this->upload->data();
				if($this->_thumb($img['full_path'])){
					return $img['file_name'];
				}
			}

		} else
			redirect(base_url() . 'admin/login');
	}

	function _thumb($img){
		if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
			$config['image_library'] = 'gd2';
			$config['source_image'] = $img;
			$config['new_image'] = 'uploads/product/thumbs';
			$config['cread_thumb'] = TRUE;
			$config['maintain_ratio'] = TRUE;
			$config['width'] = 300;
			$config['height'] = 240;
			$this->load->library('image_lib',$config);
			if(!$this->image_lib->resize())
				return false;
			else
				return true;

		}else{
			redirect(base_url() . 'admin/login');
		}
	}
}
